package com.fmy.mcsecurity.config.filter;

import org.springframework.lang.Nullable;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName UsernamePassParamsHolder
 * @Description TODO UsernamePassParamsHolder.class
 * @createTime 2019/10/23 12:18
 */
public class UsernamePassParamsHolder {
    private final static ThreadLocal<Map<String, Object>> THREADLOCAL = new ThreadLocal<>();

    public static Map<String, Object> get() {
        return THREADLOCAL.get();
    }

    public static void set(Map<String, Object> params) {
        THREADLOCAL.set(params);
    }

    public static void set(String k, Object v) {
        Map<String, Object> params = get();
        if (params == null) {
            params = new HashMap<>();
            set(params);
        }
        params.put(k, v);
    }

    public static void clear() {
        THREADLOCAL.remove();
    }

    public static Object getKey(String key) {
        Map<String, Object> params = get();
        return params != null ? params.get(key) : null;
    }

    public static String getLoginType() {
        Object type = getKey(UsernamePassParamsFilter.getTypeKey());
        if (type == null) {
            return null;
        }
        return type.toString();
    }

    static void setLoginType(String type) {
        set(UsernamePassParamsFilter.getTypeKey(), type);
    }

    static void setClient(String client) {
        set(UsernamePassParamsFilter.getClientKey(), client);
    }

    public static String getClient() {
        Object client = getKey(UsernamePassParamsFilter.getClientKey());
        if (client == null) {
            return null;
        }
        return client.toString();
    }

    public static String getUsername() {
        Object username = getKey(UsernamePassParamsFilter.getUsername());
        if (username == null) {
            return null;
        }
        return username.toString();
    }

    static void setUsername(String username) {
        set(UsernamePassParamsFilter.getUsername(), username);
    }
}